#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int i=0;
char *listdb[100];

struct list
{
           float n;
           struct list *nx;
}*start[100]={NULL};


void insertinx(float n,int x,int k)
{    int o;
     struct list *alloc,*node;
     alloc=(struct list*)malloc(sizeof(struct list));
     if(alloc==NULL){return;}
     alloc->n=n;
     if(k==1){alloc->nx=start[x]; start[x]=alloc; return;}
     for(o=k-2,node=start[x];node->nx!=NULL&&o>=1;node=node->nx,o--);
     if(o>=1)
            {printf("\nError!!!!");
                                   return;}
     alloc->nx=node->nx;
     node->nx=alloc;
     printf("\nDone!");
     
     
}

void insert(float n,int x)
{    struct list *alloc,*node;
     alloc=(struct list*)malloc(sizeof(struct list));
     alloc->n=n;
     alloc->nx=NULL;
     if(start[x]==NULL)start[x]=alloc;
     else
     {
         for(node=start[x];node->nx!=NULL;node=node->nx);
         node->nx=alloc;
         }
}
void print(int x)
{    struct list *node;
	printf("\n%s=",listdb[x]);
     for(node=start[x];node!=NULL;node=node->nx)
                                             printf("%0.1f,",node->n);
} 
void pop(int x,int k)
{    struct list *node;
     int o=k;
     if(start[x]==NULL){printf("\nEmpty!!"); return;}
     if(k==0){;if(start[x]->nx!=NULL){for(node=start[x];node->nx->nx!=NULL;node=node->nx);
                                                                                     node->nx=node->nx->nx;}
               else
                   start[x]=NULL; 
                                     
                   return;}                
          if(k==1){
             start[x]=start[x]->nx; return;}
     for(o=k-2,node=start[x];node->nx->nx!=NULL&&o>=1;node=node->nx,o--);
     if(o>=1)
            {printf("\nError!!!!");
                                   return;}

     else
             node->nx=node->nx->nx;
     
 }
void rev(int x){
	struct list *st,*en,*tem;
	float tmp;	
	st=start[x];
	printf("\nhello");
	for(en=start[x];en->nx!=NULL;en=en->nx);
	while(en!=st&&en->nx!=st)
	{
		tmp=st->n;
		st->n=en->n;
		en->n=tmp;
		st=st->nx;
		tem=en;
		for(en=start[x];en->nx!=tem&&en!=NULL;en=en->nx);

	}
	printf("\nReversed string:");
	    printf("\n");
	print(x);
	

}
void extend(float x[],int n,int k)
{    int o;
     for(o=0;o<k;o++)
                            insert(x[o],n);
}
void extend1(int k,int n)
{
                 struct list *node1,*node2;
                 node1=start[k];
                 node2=start[n];
                 for(;node1!=NULL;node1=node1->nx);
                 for(;node2!=NULL;node2=node2->nx)
                 {
                                                                 insert(node2->n,k);
                                                                 
                                                                 }}
                 
void newl(float x[20],int n)
{    int o;
     struct list *node;
     node=(struct list *)malloc(sizeof(struct list));
     for(o=0;o<n;o++){
                     insert(x[o],i);}
     i++;
     
}
void indx(int x,float k)
{
     struct list *node;
     node=start[x];
     int o=1;
     for(;node!=NULL;node=node->nx,o++)
                                   if(node->n==k)
                                   { printf("\n%d Position!",o); return;}
     if(node==NULL)
     printf("\nNot Found!");}
void remv(float x,int l)
{     struct list *node=start[l];
     if(node->n==x)
     {
                   start[l]=start[l]->nx;
                   return;
                   }
     for(;node->nx!=NULL;node=node->nx)
     {
              if(node->nx->n==x)
		{              node->nx=node->nx->nx;
				return;}
     }
	if(node->n==x){node=NULL; return;}
    printf("\nNot found!!");	
				
                                
}
void count(int x,float k)
{
     int o=0;
     struct list *node;
     node=start[x];
     for(;node!=NULL;node=node->nx)
                                  if(node->n==k)
                                                o++;
     printf("\n%d times!",o);}
int decode(char c[])
{    int m;
     for(m=0;m<=i;m++)
                      if(strcmp(c,listdb[m])==0){
                                                 return m;}
}

void bubsort(int x)
{
     struct list *st,*sr,*sm,*sp;
     float tmp;
     int flag;
     do{flag=0;
     for(st=start[x],sr=start[x]->nx;sr!=NULL;st=st->nx,sr=sr->nx)
     {
                                              if(st->n>sr->n)
                                              {tmp=st->n;
                                              st->n=sr->n;
                                              sr->n=tmp;
                                              flag=1;
                                              }
                                              }
                                              }while(flag==1);
     printf("\nSorted list!");    printf("\n");
     
     print(x);
}
void databprint()
{
	int o;
	for(o=0;o<i;o++)
		
		print(o);
	printf("\n");
}                               
                   
int main()
{   int i1;
    char g[20];
    float n;
    do{r1:
     printf("\n1.Start new list.\n2.Append\n3.print\n4.Delete\n5.extend\n6.Insert at x.\n7.Reverse\n8.POP\n9.Index\n10.Count.\n11.Sort\n12.Exit");
     scanf("%d",&i1);
     if(i1==1){
              int j=0,k=0;
              int i2=0;
              char a[100],c[20],b1[20];
           printf("\nEnter the name:");
           scanf("%s",b1);
	   listdb[i]=(char *)calloc(strlen(b1),sizeof(char));
	   strcpy(listdb[i],b1);
           printf("\nEnter list seperated by comma:");
           scanf("%s",a);
           float b[20]={0};
           for(i2=0;a[i2]!='\0';i2++)
           {               if(a[i2]!=',')
                           {     c[j++]=a[i2];                                 
                           }
                           else
                           {   c[j]='\0';
                               b[k++]=atof(c);
                               j=0;
                               strcpy(c,"0");                                                                
                               }
                               }
           b[k++]=atof(c);
           newl(b,k);}
     else if(i1==2){ 
                     printf("\nEnter the name:");
                     scanf("%s",g);
                     printf("\nEnter the element:");
                     scanf("%f",&n);
                     insert(n,decode(g));}
                     
     else if(i1==3){
                   printf("\nEnter the name:");
                   scanf("%s",g);
                   print(decode(g));
		   goto r1;
                   }
     else if(i1==4){
                    printf("\nEnter the name:");
                    scanf("%s",g);
                    printf("\nElement:");
                    scanf("%f",&n);
                    remv(n,decode(g));}
     else if(i1==5){int i12;
                    printf("\nEnter the name:");
                    scanf("%s",g);
                    printf("\n1.existing list.\n2.New list");
                    scanf("%d",&i12);
                    if(i12==1){
                              char t[20];
                              printf("\nEnter name:");
                              scanf("%s",t);
                              extend1(decode(g),decode(t));}
                    else{
                              
                    int j=0,k=0;
                    int i2=0;
                    char a[100],c[20];
                    printf("\nEnter list seperated by comma:");
                    scanf("%s",a);
                    float b[20]={0};
                    for(i2=0;a[i2]!='\0';i2++)
                    {               if(a[i2]!=',')
                                           {     c[j++]=a[i2];                                 
                                           }
                                           else
                                           {   c[j]='\0';
                                            b[k++]=atof(c);
                                                j=0;
                                                   strcpy(c,"0");                                                                
                                                      }
                                                      }
                                                      b[k++]=atof(c);
                    extend(b,decode(g),k);}}
     else if(i1==6){printf("\nEnter the name:");
                    scanf("%s",g);
                    printf("\nElement:");
                    scanf("%f",&n);
                    int j;
                    printf("\nPosition:");
                    scanf("%d",&j);
                    insertinx(n,decode(g),j);}
     else if(i1==7){printf("\nEnter the name:");
                    scanf("%s",g);
                    rev(decode(g));
			goto r1;}
     else if(i1==8){printf("\nEnter the name:");
                    scanf("%s",g);
                    printf("\nEnter the position(press enter to skip):");
                    
                    char h[4]={0},ch;
                    int i4=0;
                    int t;
                    printf("\nenter");
                    setbuf(stdin,NULL);
                    while ((ch = getchar()) != '\n')
                    {                         h[i4]=ch;
                                     i4++;
                                       }h[i4]='\0';
                                       if(i4>0)t=atoi(h);
                         		else t=-1;               
                    pop(decode(g),t+1);}
     else if(i1==9){printf("\nEnter the name:");
                    scanf("%s",g);
                    float k;
                    printf("\nEnter the number:");
                    scanf("%f",&k);
                    indx(decode(g),k);}
     else if(i1==10){printf("\nEnter the name:");
                    scanf("%s",g);
                    float k;
                    printf("\nEnter the number:");
                    scanf("%f",&k);
                    count(decode(g),k);}
     else if(i1==11){printf("\nEnter the name:");
                    scanf("%s",g);
                    bubsort(decode(g));
			goto r1;}
printf("\n///////////////////DATABASE\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
     databprint();
     }while(i1!=12);
     
}


